<script setup lang="ts">
import { ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps, usePublicHooks } from "@/views/hooks";
import { WorkId } from "@/api/workId";

const props = withDefaults(defineProps<FormProps<WorkId>>(), {
  formInline: () => ({
    id: "",
    name: "",
    prefix: "",
    suffix: "",
    begin: 1,
    current: 1,
    next: 2,
    description: "",
    status: true
  })
});
const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const disabled = !!newFormInline.value.id;

function getRef() {
  return ruleFormRef.value;
}
const changeBegin = (value: number) => {
  newFormInline.value.current = value;
  changeCurrent(value);
};
const changeCurrent = (value: number) => {
  newFormInline.value.next = value + 1;
};

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="工号名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入工号名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item v-if="!disabled" label="工号起始" prop="begin">
          <el-input-number
            v-model="newFormInline.begin"
            clearable
            placeholder="请输入工号起始"
            :min="1"
            :step="1"
            controls-position="right"
            @change="changeBegin"
          />
        </el-form-item>
        <el-form-item v-if="disabled" label="工号起始">
          <el-input-number
            v-model="newFormInline.begin"
            :controls="false"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="工号前缀" prop="prefix">
          <el-input
            v-model="newFormInline.prefix"
            clearable
            placeholder="请输入工号前缀"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="工号后缀" prop="suffix">
          <el-input
            v-model="newFormInline.suffix"
            clearable
            placeholder="请输入工号后缀"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="当前工号" prop="current">
          <el-input-number
            v-model="newFormInline.current"
            clearable
            placeholder="请输入当前工号"
            :min="newFormInline.begin"
            :step="1"
            controls-position="right"
            @change="changeCurrent"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="下一工号" prop="next">
          <el-input-number
            v-model="newFormInline.next"
            clearable
            placeholder="请输入下一工号"
            :min="newFormInline.begin"
            :step="1"
            :readonly="true"
            :controls="false"
          />
        </el-form-item>
      </re-col>
      <re-col>
        <el-form-item label="工号描述" prop="description">
          <el-input
            v-model="newFormInline.description"
            clearable
            placeholder="请输入工号描述"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="工号状态">
          <el-switch
            v-model="newFormInline.status"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
